package org.jeecg.modules.wmhb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.common.constant.UserConstant;
import org.jeecg.modules.wmhb.entity.*;
import org.jeecg.modules.wmhb.mapper.WmProductBomMapper;
import org.jeecg.modules.wmhb.service.IWmProductBomService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 物料产品BOM表
 * @Author: jeecg-boot
 * @Date:   2024-08-31
 * @Version: V1.0
 */
@Service
public class WmProductBomServiceImpl extends ServiceImpl<WmProductBomMapper, WmProductBom> implements IWmProductBomService {
    @Override
    public void setConfirmByBatchNo(String batchNo){
        LambdaUpdateWrapper<WmProductBom> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WmProductBom::getEnableFlag, UserConstant.ENABLE_FLAG_YES).eq(WmProductBom::getBatchNo, batchNo);
        update(null, updateWrapper);
    };

    @Override
    public void importWmPurchaseTaskbookBom(PurchaseBom purchaseBom, WmTask wmTaskParent, WmItem wmItemImport){
        WmProductBom wmProductBom = new WmProductBom();
        wmProductBom.setBomItemId(wmItemImport.getId());
        wmProductBom.setBomItemSpec(wmItemImport.getSpecification());
        wmProductBom.setBomItemCode(wmItemImport.getCode());
        wmProductBom.setBomItemName(wmItemImport.getName());
        wmProductBom.setUnitOfMeasure(wmItemImport.getUnitOfMeasure());
        LambdaQueryWrapper<WmProductBom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmProductBom::getWmItemId, wmTaskParent.getWmItemId()).eq(WmProductBom::getBomItemId, wmItemImport.getId()).last("LIMIT 1");
        WmProductBom wmProductBomExist = getOne(queryWrapper);
        if (wmProductBomExist == null) {
            //存在则不添加
            wmProductBom.setBatchNo(wmItemImport.getBatchNo());
            wmProductBom.setItemOrProduct(wmItemImport.getItemOrProduct());
            wmProductBom.setEnableFlag(UserConstant.ENABLE_FLAG_NO);
            wmProductBom.setWmItemId(wmTaskParent.getWmItemId());//父产品ID
            wmProductBom.setQuantity(purchaseBom.getQuantity());
            save(wmProductBom);
        }
    };

    @Override
    public void importComponentBom(BOMNode bomNode, WmItem wmItemParent, WmItem wmItemImport){
        WmProductBom wmProductBom = new WmProductBom();
        wmProductBom.setBomItemId(wmItemImport.getId());
        wmProductBom.setBomItemSpec(wmItemImport.getSpecification());
        wmProductBom.setBomItemCode(wmItemImport.getCode());
        wmProductBom.setBomItemName(wmItemImport.getName());
        wmProductBom.setUnitOfMeasure(wmItemImport.getUnitOfMeasure());
        LambdaQueryWrapper<WmProductBom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmProductBom::getWmItemId, wmItemParent.getId()).eq(WmProductBom::getBomItemId, wmProductBom.getBomItemId()).last("LIMIT 1");
        WmProductBom wmProductBomExist = getOne(queryWrapper);
        if (wmProductBomExist == null) {
            //存在则不添加
            wmProductBom.setBatchNo(wmItemImport.getBatchNo());
            wmProductBom.setItemOrProduct(wmItemImport.getItemOrProduct());
            wmProductBom.setEnableFlag(UserConstant.ENABLE_FLAG_NO);
            wmProductBom.setWmItemId(wmItemParent.getId());
            wmProductBom.setQuantity(bomNode.getQuantity());
            save(wmProductBom);
        }
    };

    @Override
    public WmProductBom saveItemWhenAddTask(WmItem wmItem, WmTask taskParent, WmTask wmTask){
        WmProductBom wmProductBom = new WmProductBom();  //初始化产品物料
        //设置产品物料
        wmProductBom.setBomItemId(wmItem.getId());
        wmProductBom.setBomItemSpec(wmItem.getSpecification());
        wmProductBom.setBomItemCode(wmItem.getCode());
        wmProductBom.setBomItemName(wmItem.getName());
        wmProductBom.setUnitOfMeasure(wmItem.getUnitOfMeasure());
        if (taskParent != null) {
            LambdaQueryWrapper<WmProductBom> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WmProductBom::getWmItemId, taskParent.getWmItemId()).eq(WmProductBom::getBomItemId, wmProductBom.getBomItemId());
            WmProductBom wmProductBomExist = getOne(queryWrapper);
            if (wmProductBomExist == null) {
                //存在则不添加
                wmProductBom.setItemOrProduct(wmItem.getItemOrProduct());
                wmProductBom.setEnableFlag(UserConstant.ENABLE_FLAG_NO);
                wmProductBom.setWmItemId(taskParent.getWmItemId());
                wmProductBom.setQuantity(wmTask.getQuantity());
                save(wmProductBom);
            }
        }

        return wmProductBom;
    };

    @Override
    public void importContractProductBom(Double quantity, WmItem wmItemParent, WmItem wmItemImport){
        WmProductBom wmProductBom = new WmProductBom();                    //初始化产品物料
        //设置产品物料
        wmProductBom.setBomItemId(wmItemImport.getId());
        wmProductBom.setBomItemSpec(wmItemImport.getSpecification());
        wmProductBom.setBomItemCode(wmItemImport.getCode());
        wmProductBom.setBomItemName(wmItemImport.getName());
        wmProductBom.setUnitOfMeasure(wmItemImport.getUnitOfMeasure());
        //4、创建产品BOM
        if (wmItemParent != null) {
            LambdaQueryWrapper<WmProductBom> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WmProductBom::getWmItemId, wmItemParent.getId()).eq(WmProductBom::getBomItemId, wmItemImport.getId());
            WmProductBom wmProductBomExist = getOne(queryWrapper);
            if (wmProductBomExist == null) {
                //存在则不添加
                wmProductBom.setBatchNo(wmItemImport.getBatchNo());
                wmProductBom.setItemOrProduct(wmItemImport.getItemOrProduct());
                wmProductBom.setEnableFlag(UserConstant.ENABLE_FLAG_NO);
                wmProductBom.setWmItemId(wmItemParent.getId());
                wmProductBom.setQuantity(quantity);
                save(wmProductBom);
            }
        }
    };

    @Override
    public List<WmProductBom> queryChildrenByWmItemId(String wmItemId){
        LambdaQueryWrapper<WmProductBom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmProductBom::getWmItemId, wmItemId);
        return list(queryWrapper);
    };
}
